package shapewar.net.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.util.Date;

import shapewar.net.io.Tool;
import shapewar.net.seriz.CreateInfo;
import shapewar.net.seriz.DieInfo;
import shapewar.net.seriz.PosInfo;
import shapewar.net.seriz.Info;
import shapewar.net.seriz.MoveInfo;
import shapewar.net.seriz.NetConf;
import shapewar.net.seriz.ReliveInfo;
import shapewar.net.seriz.SynchInfo;
import shapewar.net.seriz.TextInfo;

public class OIS {
	ObjectInputStream ois;
	InputStream is;
	public OIS(InputStream is) throws IOException {
		ois = new ObjectInputStream(is);
		this.is = is;
	}
	
	public Info readObject() throws OptionalDataException, ClassNotFoundException, IOException,ArrayIndexOutOfBoundsException,Exception{
		byte b[] = new byte[1024];
		is.read(b,0,8);
		int val[] = Tool.depart(b);
		String str = Tool.departs(b);
		switch(val[0]){
		case NetConf.info_create:
		case NetConf.info_move:
		case NetConf.info_flash:
		case NetConf.info_die:
		case NetConf.info_protectorMinus:
		case NetConf.info_exit:
		case NetConf.info_getId:
		case NetConf.info_relive:
		case NetConf.info_toast:
		case NetConf.info_synch:
			is.read(b, 8, is.available());
			val = Tool.depart(b);
			str = Tool.departs(b);
			break;
		default:
			Info in = new Info();
			in = (Info) ois.readObject();
			return in;
		}
		switch(val[0]){
		case NetConf.info_create:
			CreateInfo ci = new CreateInfo();
			ci.cmd = val[0];
			ci.id = val[1];
			ci.x = val[2];
			ci.y = val[3];
			ci.target_x = val[4];
			ci.target_y = val[5];
			ci.state = val[6];
			ci.protector_num = val[7];
			ci.killNum = val[8];
			ci.dieNum = val[9];
			ci.map_width = val[10];
			ci.map_height = val[11];
			ci.speed = val[12]/100f;
			ci.room_id = val[13];
			String arr[] = Tool.departs(b).split(",");
			ci.name = arr[0];
			ci.mac = arr[1];
			return ci;
		case NetConf.info_move:
			MoveInfo mi = new MoveInfo();
			mi.cmd = val[0];
			mi.id = val[1];
			mi.target_x = val[2];
			mi.target_y = val[3];
			mi.blt_key = val[4];
			mi.x = val[5];
			mi.y = val[6];
			return mi;
		case NetConf.info_flash:
			PosInfo fi = new PosInfo();
			fi.cmd = val[0];
			fi.id = val[1];
			fi.x = val[2];
			fi.y = val[3];
			return fi;
		case NetConf.info_die:
			DieInfo di = new DieInfo();
			di.cmd = val[0];
			di.id = val[1];
			di.killerId = val[2];
			di.killedDieNum = val[3];
			di.killerKillNum = val[4];
			di.ptrNum = val[5];
			return di;
		case NetConf.info_protectorMinus:
			DieInfo di1 = new DieInfo();
			di1.cmd = val[0];
			di1.id = val[1];
			di1.killerId = val[2];
			di1.bulletKey = val[3];
			return di1;
		case NetConf.info_exit:
			Info info1 = new Info();
			info1.cmd = val[0];
			info1.id = val[1];
			return info1;
		case NetConf.info_getId:
			Info gii = new Info();
			gii.cmd = val[0];
			gii.id = val[1];
			return gii;
		case NetConf.info_relive:
			ReliveInfo ri = new ReliveInfo();
			ri.cmd = val[0];
			ri.id = val[1];
			ri.x = val[2];
			ri.y = val[3];
			return ri;
		case NetConf.info_toast:
			TextInfo ti = new TextInfo();
			ti.cmd = val[0];
			ti.id = val[1];
			ti.str = str;
			return ti;
		case NetConf.info_synch:
			SynchInfo si = new SynchInfo();
			si.cmd = val[0];
			si.length = val[1];
			si.size = val[2];
			si.infos = new int[si.length][si.size];
			int k = 3;
			for(int i=0;i<si.length;i++){
				for(int j=0;j<si.size;j++){
					si.infos[i][j] = val[k++];
				}
			}
			return si;
		}
		return null;
	}
	
}
